Ext.ns('Portal.userScreen');

Ext.apply(Portal.userScreen, {

    //functie de stergere / editare inregistrare
    action: function () {
        var oGrid, iRec = 0, oWind, op = "edit";

        oGrid = Ext.getCmp(Portal.userScreen.id).getComponent("userGrid");

        //setare tip operatie; daca este butonul de adaugare / editare atunci se citeste paramentrul `action` al acestuia
        if (this.action) {
            op = this.action;
        }

        if (op === "edit") {
            if (oGrid.selModel.getCount() != 1) {
                Portal.events.throwInformation("Selectati o inregistrare !");
                return;
            }
            iRec = oGrid.selModel.getSelected().id;
        }

        //constructie formular
        var oFpAdd = new Ext.FormPanel({
            autoHeight: true, //inaltimea formului va da inaltimea ferestrei
            labelWidth: 160,
            frame: true,
            bodyStyle: {
                padding: "5px"
            },
            layout: 'form',
            defaults: {
                anchor: '90%',
                xtype: "textfield",
                allowBlank: false
            },
            items: [
                {
                    name: 'LastName',
                    fieldLabel: 'Nume'
                },
                {
                    name: 'FirstName',
                    fieldLabel: 'Prenume'
                },
                {
                    vtype: 'email',
                    name: 'Email',
                    fieldLabel: 'Email'
                },
                {
                    name: 'Username',
                    fieldLabel: 'Username'
                },
                {
                    fieldLabel: "Nivel Acces",
                    xtype: "uxFCombo",
                    editable: false,
                    name: "AccessLevel",
                    easyConfig: {
                        mode: "local",
                        localData: [
                            ['user', 'user'],
                            ['admin', 'admin'],
                            ['supervisor', 'supervisor']
                        ]
                    }
                },
                {
                    xtype: 'xcheckbox',
                    name: "Available",
                    fieldLabel: "Disponibil"
                },
                {
                    fieldLabel: "Parola",
                    name: 'Password',
                    allowBlank: true
                },
                {
                    name: "Id",
                    xtype: "hidden",
                    value: iRec
                }
            ],
            buttons: [
                {
                    text: 'Salvare',
                    handler: function () {

                        if (!oFpAdd.getForm().isValid()) {
                            return;
                        }

                        oFpAdd.getForm().submit({
                            url: "Users/EditUser",
                            success: function (form, response) {
                                oGrid.store.reload();
                                oWind.close();
                            }
                        });
                    }
                }
            ]
        });

        //constructie fereastra
        oWind = new Ext.Window({
            title: (op === "edit") ? "Editare inregistrare" : "Adaugare inregistrare",
            width: 450,
            layout: 'fit',
            modal: true,
            closeAction: 'close',
            items: [oFpAdd]
        }).show();

        //daca este editare de inregistrare atunci se aduc datele acestei inregistrari
        if (op === "edit") {
            oFpAdd.on("afterlayout", function () {
                        Ext.Ajax.request({
                            url: 'Users/GetUsersById',
                            params: {
                                Id: iRec
                            },
                            success: function (response) {
                                var oRes = Ext.decode(response.responseText);
                                var oData = oRes.Records;
                                fnCompleteForm(oFpAdd, oData[0]);  //vezi utils.js
                            }
                        })
                    }, this, { single: true });
        }
    }
});

Ext.apply(Portal.userScreen, {

    id: 'idTabUserScreen',
    exec: function () {

        if (!Ext.getCmp(Portal.userScreen.id)) {

            var oCmUser = new Ext.grid.ColumnModel({
                columns: [
                    {
                        header: "Nume",
                        dataIndex: 'LastName',
                        width: 100
                    },
                    {
                        header: "Prenume",
                        dataIndex: 'FirstName',
                        width: 100
                    },
                    {
                        header: "Username",
                        dataIndex: 'Username'
                    },
                    {
                        header: "Email",
                        dataIndex: 'Email'
                    }
                    ,
                    {
                        header: "Rol",
                        dataIndex: 'AccessLevel'
                    },
                    {
                        header: 'Activ',
                        dataIndex: 'Available',
                        width: 50,
                        renderer: Portal.renderer.available
                    }
                ],
                defaults: {
                    sortable: true
                }
            });

            var oUserGrid = new Ext.ux.fastGrid({
                xtype: "uxFGrid",
                layout: 'fit',
                itemId: "userGrid",
                region: 'center',
                gridConfig: {
                    url: 'Users/GetUsers',
                    fields: ['Id', 'LastName', 'FirstName', 'Username', 'Email', 'AccessLevel', 'Available'],
                    viewConfig: {
                        getRowClass: function (record, index) {
                            var cls = "", c;
                            c = record.get('available');
                            if (c == 0) {
                                cls = cls + ' inactive-row';
                            }
                            return cls;
                        }
                    }
                },
                cm: oCmUser,
                tbar: [
                    {
                        text: "Adauga",
                        iconCls: 'icon-add',
                        action: "add",
                        handler: Portal.userScreen.action
                    },
                    "-",
                    {
                        text: "Editeaza",
                        iconCls: 'icon-cog-edit',
                        action: "edit",
                        handler: Portal.userScreen.action
                    }
                ],
                listeners: {
                    'rowdblclick': Portal.userScreen.action
                }
            });

            //creare tab ce contine gridul
            var oNomTab = new Ext.Panel({
                title: 'Utilizatori',
                id: Portal.userScreen.id,
                iconCls: 'icon-group',
                closable: true,
                layout: 'border',
                items: [oUserGrid]
            });

            //adaugare tab la tabpanel-ul principal
            Portal.oCenterRegion.add(oNomTab);
            Portal.oCenterRegion.doLayout();

        }
        //activare tab
        Portal.oCenterRegion.setActiveTab(Portal.userScreen.id);
    }
});